package saucaca.fragment;

import saucaca.Adapter.ArtistAdapter;
import saucaca.JSON.WServiceLib;
import saucaca.Util.CMD;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class AppFragment extends Fragment {
	MainActivity mActivity;
	ListView listview;
	boolean loadingMore = false;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	String page = "0";
	String pageSize = "";
	int xpage = 0;
	ArtistAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.listview2, container, false);
		findViewById(view);
		new GetFromServices().execute("");
		mActivity.myActionBar.setMenu(R.string.menu_ungdung, false, true);
		return view;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (!hidden)
			mActivity.myActionBar.setMenu(R.string.menu_ungdung, false, true);
	}

	public void findViewById(View view) {
		listview = (ListView) view.findViewById(R.id.layoutlistview_listview);
		listview.setVisibility(View.VISIBLE);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		listview.setEmptyView(mView_EmptyLoading);
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... param) {
			page = Integer.toString(xpage);
			loadingMore = true;
			String str = WServiceLib.notification(page, pageSize, CMD.NOTIFICATION_TYPE_CLOUD);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				BaseEngine.showLogformLongString(result);
		}
	}
}
